﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using Module.Common.Models;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class AdvertisementPositionsDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion


        #region Methods

        /// <summary>
        /// Saves a record to the Advertisement_Positions table.
        /// </summary>
        public virtual void Insert(AdvertisementPositionsInfo advertisementPositionsInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
				new SqlParameter("@Title", advertisementPositionsInfo.Title),
				new SqlParameter("@Description", advertisementPositionsInfo.Description),
				new SqlParameter("@ShowMode", advertisementPositionsInfo.ShowMode),				
				new SqlParameter("@RepeatColumns", advertisementPositionsInfo.RepeatColumns),				
				new SqlParameter("@CreatedBy", advertisementPositionsInfo.CreatedBy),
				new SqlParameter("@CreatedOn", advertisementPositionsInfo.CreatedOn),
				new SqlParameter("@Width", advertisementPositionsInfo.Width),
				new SqlParameter("@Height", advertisementPositionsInfo.Height),
				new SqlParameter("@TimeDelay", advertisementPositionsInfo.TimeDelay),
				new SqlParameter("@Priority", advertisementPositionsInfo.Priority),
				new SqlParameter("@Lang", advertisementPositionsInfo.Lang),
                new SqlParameter("@GroupPostion", advertisementPositionsInfo.GroupPostion)                
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Advertisement_Positions_Insert", parameters);
        }

        /// <summary>
        /// Updates a record in the Advertisement_Positions table.
        /// </summary>
        public virtual void Update(AdvertisementPositionsInfo advertisementPositionsInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PositionID", advertisementPositionsInfo.PositionID),
				new SqlParameter("@Title", advertisementPositionsInfo.Title),
				new SqlParameter("@Description", advertisementPositionsInfo.Description),
				new SqlParameter("@ShowMode", advertisementPositionsInfo.ShowMode),
				new SqlParameter("@RepeatLayout", advertisementPositionsInfo.RepeatLayout),
				new SqlParameter("@RepeatColumns", advertisementPositionsInfo.RepeatColumns),
				new SqlParameter("@RepeatDirection", advertisementPositionsInfo.RepeatDirection),
				new SqlParameter("@CreatedBy", advertisementPositionsInfo.CreatedBy),
				new SqlParameter("@CreatedOn", advertisementPositionsInfo.CreatedOn),
				new SqlParameter("@Width", advertisementPositionsInfo.Width),
				new SqlParameter("@Height", advertisementPositionsInfo.Height),
				new SqlParameter("@TimeDelay", advertisementPositionsInfo.TimeDelay),
				new SqlParameter("@Priority", advertisementPositionsInfo.Priority),
				new SqlParameter("@Lang", advertisementPositionsInfo.Lang),
                new SqlParameter("@GroupPostion", advertisementPositionsInfo.GroupPostion)                
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Advertisement_Positions_Update", parameters);
        }

        /// <summary>
        /// Deletes a record from the Advertisement_Positions table by its primary key.
        /// </summary>
        public virtual void Delete(int positionID)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PositionID", positionID)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Advertisement_Positions_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the Advertisement_Positions table.
        /// </summary>
        public virtual AdvertisementPositionsInfo Select(int positionID)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PositionID", positionID)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Advertisement_Positions_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeAdvertisementPositionsInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Selects all records from the Advertisement_Positions table.
        /// </summary>
        public CHRTList<AdvertisementPositionsInfo> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Advertisement_Positions_SelectAll"))
            {
                CHRTList<AdvertisementPositionsInfo> advertisementPositionsInfoList = new CHRTList<AdvertisementPositionsInfo>();
                while (dataReader.Read())
                {
                    AdvertisementPositionsInfo advertisementPositionsInfo = MakeAdvertisementPositionsInfo(dataReader);
                    advertisementPositionsInfoList.Add(advertisementPositionsInfo);
                }

                return advertisementPositionsInfoList;
            }
        }

        /// <summary>
        /// Creates a new instance of the Advertisement_Positions class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual AdvertisementPositionsInfo MakeAdvertisementPositionsInfo(SqlDataReader dataReader)
        {
            AdvertisementPositionsInfo advertisementPositionsInfo = new AdvertisementPositionsInfo();
            advertisementPositionsInfo.PositionID = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.POSITIONID, 0);
            advertisementPositionsInfo.Title = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.TITLE, String.Empty);
            advertisementPositionsInfo.Description = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.DESCRIPTION, String.Empty);
            advertisementPositionsInfo.ShowMode = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.SHOWMODE, 0);
            advertisementPositionsInfo.RepeatLayout = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.REPEATLAYOUT, 0);
            advertisementPositionsInfo.RepeatColumns = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.REPEATCOLUMNS, 0);
            advertisementPositionsInfo.RepeatDirection = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.REPEATDIRECTION, 0);
            advertisementPositionsInfo.CreatedBy = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.CREATEDBY, 0);
            advertisementPositionsInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.CREATEDON, DateTime.Now);
            advertisementPositionsInfo.Width = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.WIDTH, 0);
            advertisementPositionsInfo.Height = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.HEIGHT, 0);
            advertisementPositionsInfo.TimeDelay = SqlClientUtility.GetFloat(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.TIMEDELAY, 0.0F);
            advertisementPositionsInfo.Priority = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.PRIORITY, 0);
            advertisementPositionsInfo.Lang = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.LANG, 0);
            advertisementPositionsInfo.GroupPostion = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT_POSITIONS.GroupPostion, 0);

            return advertisementPositionsInfo;
        }

        #endregion
    }
}
